package com.awcall.iot.controller;


import com.awcall.iot.common.BaseController;
import com.awcall.iot.common.ResponseResult;
import com.awcall.iot.model.Project;
import com.awcall.iot.service.IProjectService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wuhui
 * @since 2018-03-23
 */
@RestController
@RequestMapping("/admin/project")
public class ProjectController extends BaseController {

    @Autowired
    private IProjectService iProjectService;

    @GetMapping("/index")
    public ResponseResult index(Project project, Page<Project> page) {

        EntityWrapper<Project> entityWrapper = new EntityWrapper<>();

        if (StringUtils.isEmpty(project.getName())) {
            entityWrapper.like(Project.NAME, project.getName());
        }

        Page<Project> projectPage = iProjectService.selectPage(page, entityWrapper);

        return new ResponseResult<>(projectPage);
    }

    @GetMapping("/list")
    public ResponseResult list(Project project) {

        EntityWrapper<Project> entityWrapper = new EntityWrapper<>();

        if (StringUtils.isEmpty(project.getName())) {
            entityWrapper.like(Project.NAME, project.getName());
        }

        List<Project> projects = iProjectService.selectList(entityWrapper);

        return new ResponseResult<>(projects);
    }

    @GetMapping("/detail")
    public ResponseResult detail(Project project) {

        Project project1 = project.selectById();

        return new ResponseResult<>(project1);
    }

    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody Project project) {

        boolean insert = project.insert();

        if (insert) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody Project project) {

        boolean updateById = project.updateById();

        if (updateById) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody Project project) {

        boolean deleteById = project.deleteById();

        if (deleteById) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

}
